<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="G1hRwH33Kv">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14.0/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"dockerlin666.gitee.com","root":"/","scheme":"Muse","version":"8.0.0-rc.5","exturl":false,"sidebar":{"position":"left","display":"always","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"changyan","storage":true,"lazyload":false,"nav":null,"activeClass":"changyan"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false};
  </script>

  <meta name="description" content="介绍log4sh是shell脚本的日志框架，它的工作原理与Apache Software Foundation提供的其他优秀日志产品类似(例如log4j、log4perl)。尽管功能不如其他命令强大，但它可以简化向shell脚本添加高级日志记录的任务，并且比基本的“echo”命令强大得多。此外，还可以从.properties文件配置它，这样生产环境中的脚本就不需要更改它们生成的日志记录的数量。项目">
<meta property="og:type" content="article">
<meta property="og:title" content="shell下记录日志log4jsh">
<meta property="og:url" content="http://dockerlin666.gitee.com/2020/08/24/shell-log4jsh/index.html">
<meta property="og:site_name" content="阳光灿烂的日志">
<meta property="og:description" content="介绍log4sh是shell脚本的日志框架，它的工作原理与Apache Software Foundation提供的其他优秀日志产品类似(例如log4j、log4perl)。尽管功能不如其他命令强大，但它可以简化向shell脚本添加高级日志记录的任务，并且比基本的“echo”命令强大得多。此外，还可以从.properties文件配置它，这样生产环境中的脚本就不需要更改它们生成的日志记录的数量。项目">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-08-24T07:59:19.000Z">
<meta property="article:modified_time" content="2020-09-16T03:22:23.447Z">
<meta property="article:author" content="刀客林">
<meta property="article:tag" content="shell">
<meta property="article:tag" content="log4jsh">
<meta property="article:tag" content="log">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://dockerlin666.gitee.com/2020/08/24/shell-log4jsh/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>shell下记录日志log4jsh | 阳光灿烂的日志</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?f59280177ba9fcb78a3c938de71308e3";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <main class="main">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader">
        <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">
      <img class="custom-logo-image" src="/uploads/logo400.png" alt="阳光灿烂的日志">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">阳光灿烂的日志</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">oh,my god</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a>

  </li>
  </ul>
</nav>




</div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.</span> <span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E7%AC%94%E8%AE%B0"><span class="nav-number">2.</span> <span class="nav-text">使用笔记</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%90%AD%E5%BB%BA%E7%8E%AF%E5%A2%83"><span class="nav-number">2.1.</span> <span class="nav-text">搭建环境</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#hello-world"><span class="nav-number">2.2.</span> <span class="nav-text">hello world</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%AB%98%E7%BA%A7%E9%85%8D%E7%BD%AE"><span class="nav-number">2.3.</span> <span class="nav-text">高级配置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8F%AF%E9%85%8D%E7%BD%AE%E5%8F%82%E6%95%B0"><span class="nav-number">2.3.1.</span> <span class="nav-text">可配置参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%9A%E4%B9%89log-properties-%E6%96%87%E4%BB%B6"><span class="nav-number">2.3.2.</span> <span class="nav-text">定义log properties 文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E6%96%87%E4%BB%B6%E4%BF%9D%E5%AD%98"><span class="nav-number">2.3.3.</span> <span class="nav-text">使用文件保存</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AE%B0%E5%BD%95%E6%97%A5%E5%BF%97%E6%97%B6%E9%97%B4"><span class="nav-number">2.3.4.</span> <span class="nav-text">记录日志时间</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%B0%81%E8%A3%85"><span class="nav-number">2.4.</span> <span class="nav-text">项目中封装</span></a></li></ol></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="刀客林"
      src="/uploads/avatar.jpg">
  <p class="site-author-name" itemprop="name">刀客林</p>
  <div class="site-description" itemprop="description">记录无聊的生活，要不然都忘记了！</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">6</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </section>
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </header>

      
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div id="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


      <div class="main-inner">
        

        <div class="content post posts-expand">
          

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://dockerlin666.gitee.com/2020/08/24/shell-log4jsh/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/avatar.jpg">
      <meta itemprop="name" content="刀客林">
      <meta itemprop="description" content="记录无聊的生活，要不然都忘记了！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="阳光灿烂的日志">
    </span>

    
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          shell下记录日志log4jsh
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-08-24 15:59:19" itemprop="dateCreated datePublished" datetime="2020-08-24T15:59:19+08:00">2020-08-24</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-09-16 11:22:23" itemprop="dateModified" datetime="2020-09-16T11:22:23+08:00">2020-09-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%AD%A6%E4%B9%A0/" itemprop="url" rel="index"><span itemprop="name">学习</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%AD%A6%E4%B9%A0/shell/" itemprop="url" rel="index"><span itemprop="name">shell</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2020/08/24/shell-log4jsh/#SOHUCS" itemprop="discussionUrl">
        <span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="2020/08/24/shell-log4jsh/" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>5.9k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>5 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><div class="note info"><p><strong>log4sh是shell脚本的日志框架，它的工作原理与Apache Software Foundation提供的其他优秀日志产品类似(例如log4j、log4perl)。尽管功能不如其他命令强大，但它可以简化向shell脚本添加高级日志记录的任务，并且比基本的“echo”命令强大得多。此外，还可以从.properties文件配置它，这样生产环境中的脚本就不需要更改它们生成的日志记录的数量。</strong><br><a target="_blank" rel="noopener" href="https://github.com/kward/log4sh"><span style="color:red;">项目地址</span></a> <a target="_blank" rel="noopener" href="https://github.com/kward/log4sh/blob/master/doc/log4sh.md"><span style="color:red;">手册</span></a></p>
</div>

<a id="more"></a>

<h2 id="使用笔记"><a href="#使用笔记" class="headerlink" title="使用笔记"></a>使用笔记</h2><h3 id="搭建环境"><a href="#搭建环境" class="headerlink" title="搭建环境"></a>搭建环境</h3><p>写本文的时候，版本是1.5.0</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git clone https:&#x2F;&#x2F;github.com&#x2F;kward&#x2F;log4sh.git</span><br><span class="line">mkdir test &amp;&amp; cd test</span><br><span class="line">cp ..&#x2F;log4sh&#x2F;log4sh .</span><br></pre></td></tr></table></figure>

<h3 id="hello-world"><a href="#hello-world" class="headerlink" title="hello world"></a>hello world</h3><ul>
<li>创建helloworld.sh</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#! &#x2F;bin&#x2F;bash</span><br><span class="line"></span><br><span class="line">## include log4jsh file</span><br><span class="line">. .&#x2F;log4sh</span><br><span class="line"></span><br><span class="line"># Say Hello to the world.</span><br><span class="line">logger_info &quot;Hello, world&quot;</span><br><span class="line"></span><br><span class="line"># second way to Say hello</span><br><span class="line">echo &#39;message to log&#39; |logger_info</span><br></pre></td></tr></table></figure>

<ul>
<li>创建log4j属性文件log4sh.properties </li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">#</span><br><span class="line"># log4sh example: Hello, world properties file</span><br><span class="line">#</span><br><span class="line"></span><br><span class="line"># Set root logger level to INFO and its only appender to A1.</span><br><span class="line">log4sh.rootLogger&#x3D;INFO, A1</span><br><span class="line"></span><br><span class="line"># A1 is set to be a ConsoleAppender.</span><br><span class="line">log4sh.appender.A1&#x3D;ConsoleAppender</span><br><span class="line"></span><br><span class="line"># A1 uses a PatternLayout.</span><br><span class="line">log4sh.appender.A1.layout&#x3D;PatternLayout</span><br><span class="line">log4sh.appender.A1.layout.ConversionPattern&#x3D;%-4r [%t] %-5p %c %x - %m%n</span><br></pre></td></tr></table></figure>

<ul>
<li>执行文件</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">chmod 777 * </span><br><span class="line">.&#x2F;helloworld.sh </span><br><span class="line">0 [main] INFO shell  - Hello, world</span><br><span class="line">0 [main] INFO shell  - message to log</span><br></pre></td></tr></table></figure>

<h3 id="高级配置"><a href="#高级配置" class="headerlink" title="高级配置"></a>高级配置</h3><h4 id="可配置参数"><a href="#可配置参数" class="headerlink" title="可配置参数"></a>可配置参数</h4><div class="tabs" id="configtab"><ul class="nav-tabs"><li class="tab active"><a href="#configtab-1"><i class="fa fa-bell"></i>Root Logger</a></li><li class="tab"><a href="#configtab-2"><i class="fa fa-file"></i>Layouts</a></li><li class="tab"><a href="#configtab-3"><i class="fa fa-file"></i>Environment variables</a></li></ul><div class="tab-content"><div class="tab-pane active" id="configtab-1"><p>定义输出级别和输出源名称</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">log4sh.rootLogger&#x3D;INFO, stdout</span><br><span class="line">log4sh.appender.stdout&#x3D;ConsoleAppender</span><br></pre></td></tr></table></figure>
输出INFO级别以上的log，输出源名称是stdout，stdout的输出源是ConsoleAppender

<p>可用级别Levels</p>
<ul>
<li>TRACE    The TRACE level has the lowest possible rank and is intended to turn on all logging.</li>
<li>DEBUG    The DEBUG level designates fine-grained informational events that are most useful to debug an application.</li>
<li>INFO    The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.</li>
<li>WARN    The WARN level designates potentially harmful situations.</li>
<li>ERROR    The ERROR level designates error events that might still allow the application to continue running.</li>
<li>FATAL    The FATAL level designates very severe error events that will presumably lead the application to abort.</li>
<li>OFF    The OFF level has the highest possible rank and is intended to turn off logging.</li>
</ul>
<p>可用的输出源Appenders</p>
<ul>
<li>ConsoleAppender    Output sent to console (STDOUT). </li>
<li>FileAppender    Output sent to a file.</li>
<li>DailyRollingFileAppender    Output sent to a file that rolls over daily.    partial; logs written, but not rotated</li>
<li>RollingFileAppender    Output sent to a file that rolls over by size.    partial; works, but needs improvement</li>
<li>SMTPAppender    Output sent via email.    parital; works, but needs improvement</li>
<li>SyslogAppender    Output sent to a remote syslog daemon.    partial; only localhost supported</li>
</ul>
<p>基本上使用ConsoleAppender或者FileAppender，其他的如果真有需要再研究</p>
<p>输出源可用属性</p>
<ul>
<li>DatePattern    Configure a pattern for the output filename.    no (ignored)</li>
<li>File    Output filename (special filename of STDERR used for logging to STDERR).    yes</li>
<li>MaxBackupIndex    Number of old log files to keep.    no (ignored)</li>
<li>MaxFileSize    Maximum size of old log files.    no (ignored)</li>
<li>Threshold    Logging level of the appender.    yes</li>
</ul>
<p>只有File和Threshold表明是支持的，其他的参数会忽略</p></div><div class="tab-pane" id="configtab-2"><p>可用输出格式</p>
<ul>
<li>HTMLLayout    Layout using HTML.    no (same as SimpleLayout)</li>
<li>SimpleLayout    A simple default layout (‘%p - %m’)    yes</li>
<li>PatternLayout    A patterned layout (default: ‘%d %p - %m%n’)    yes</li>
</ul>
<p>HTMLLayout和SimpleLayout是一样的</p>
<p>输出属性Pattern Options</p>
<ul>
<li>c    Used to output the category of logging request. As this is not applicable in shell, the conversion character will always returns ‘shell’.    partial (fixed)</li>
<li>d    Used to output the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces, but this specifier will be ignored. For example, %d{HH:mm:ss,SSS}, or %d{ISODATE}. The specifier is allowed only for compatibility with log4j properties files.The default format of the date returned is equivalent to the output of the Unix date command with a format of +%Y-%m-%d %H:%M:%S.    yes</li>
<li>F    Used to output the file name where the logging request was issued.The default value is equivalent basename $0.    yes</li>
<li>L    This option is for compatibility with log4j properties files.    no (ignored)</li>
<li>m    Used to output the script supplied message associated with the logging event.    yes</li>
<li>n    This option is for compatibility with log4j properties files.    no (ignored)</li>
<li>p    Used to output the priority of the logging event.    yes</li>
<li>r    Used to output the number of seconds elapsed since the start of the script until the creation of the logging event.    yes</li>
<li>t    Used to output the current executing thread. As shell doesn’t actually support threads, this is simply a value that can be set that can be put into the messages.The default value is ‘main’.    yes</li>
<li>x    This option is for compatibility with log4j properties files.    no (ignored)</li>
<li>X    Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event. The X conversion character must be followed by an environment variable name placed between braces, as in %X{clientNumber} where clientNumber is the name of the environment variable. The value in the MDC corresponding to the environment variable will be output.    yes</li>
<li>%    The sequence %% outputs a single percent sign.</li>
</ul>
<p>有些属性是不支持的，直接忽略</p></div><div class="tab-pane" id="configtab-3"><p>可用的环境变量</p>
<ul>
<li>LOG4SH_ALTERNATIVE_NC    none    Provide log4sh with the absolute path to the nc (netcat) command – e.g. /bin/nc</li>
<li>LOG4SH_CONFIGURATION    none    Provide log4sh with the absolute path to the log4sh properties file.</li>
<li>LOG4SH_CONFIG_PREFIX    log4sh    Define the expected prefix to use for parsing the properties file – e.g. log4j</li>
<li>LOG4SH_DEBUG    none    Enable internal log4sh debug output. Set to any non-empty value.</li>
<li>LOG4SH_DEBUG_FILE    none    Define a file where all internal log4sh trace/debug/info output will be written to – e.g. log4sh_internal.log</li>
<li>LOG4SH_INFO    none    Enable internal log4sh info output. Set to any non-empty value.</li>
<li>LOG4SH_TRACE    none    Enable internal log4sh trace output. Set to any non-empty value.</li>
</ul></div></div></div>


<h4 id="定义log-properties-文件"><a href="#定义log-properties-文件" class="headerlink" title="定义log properties 文件"></a>定义log properties 文件</h4><ul>
<li>编写新的配置文件custom.properties</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">log4sh.rootLogger&#x3D;DEBUG, stdout</span><br><span class="line">log4sh.appender.stdout&#x3D;ConsoleAppender</span><br></pre></td></tr></table></figure>

<ul>
<li>编写测试脚本</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">export LOG4SH_CONFIGURATION&#x3D;&#39;&#x2F;home&#x2F;cxl&#x2F;shell&#x2F;test&#x2F;custom.properties&#39;</span><br><span class="line">. .&#x2F;log4sh</span><br><span class="line"></span><br><span class="line">logger_info &quot;Hello, world:$&#123;LOG4SH_CONFIGURATION&#125;&quot;</span><br><span class="line">logger_debug &quot;Hello, world debug&quot;</span><br></pre></td></tr></table></figure>

<h4 id="使用文件保存"><a href="#使用文件保存" class="headerlink" title="使用文件保存"></a>使用文件保存</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">log4sh.rootLogger&#x3D;DEBUG, fileout</span><br><span class="line">log4sh.appender.fileout&#x3D;FileAppender</span><br><span class="line">log4sh.appender.fileout.File&#x3D;&quot;cxl.log&quot;</span><br><span class="line">log4sh.appender.fileout.Threshold&#x3D;INFO</span><br><span class="line">log4sh.appender.fileout.layout&#x3D;PatternLayout</span><br><span class="line">log4sh.appender.fileout.layout.ConversionPattern&#x3D;%c %-4r</span><br></pre></td></tr></table></figure>

<h4 id="记录日志时间"><a href="#记录日志时间" class="headerlink" title="记录日志时间"></a>记录日志时间</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">log4sh.rootLogger&#x3D;DEBUG, stdout</span><br><span class="line">log4sh.appender.stdout&#x3D;ConsoleAppender</span><br><span class="line">log4sh.appender.stdout.Threshold&#x3D;INFO</span><br><span class="line">log4sh.appender.stdout.layout&#x3D;PatternLayout</span><br><span class="line">log4sh.appender.stdout.layout.ConversionPattern&#x3D;%F %d&#123;+%Y-%m-%d %H:%M:%S&#125; %p - %m%n</span><br></pre></td></tr></table></figure>

<h3 id="项目中封装"><a href="#项目中封装" class="headerlink" title="项目中封装"></a>项目中封装</h3><p>在真实项目中使用，我写了一篇shell下的项目骨架，另文说明吧</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/shell/" rel="tag"><i class="fa fa-tag"></i> shell</a>
              <a href="/tags/log4jsh/" rel="tag"><i class="fa fa-tag"></i> log4jsh</a>
              <a href="/tags/log/" rel="tag"><i class="fa fa-tag"></i> log</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/08/05/markdown/" rel="prev" title="markdown语法介绍">
      <i class="fa fa-chevron-left"></i> markdown语法介绍
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/08/26/shell-shunit2/" rel="next" title="shell-shunit2">
      shell-shunit2 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



        </div>
        
    
  <div class="comments">
    <div id="SOHUCS"></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="http://www.beian.miit.gov.cn/" rel="noopener" target="_blank">京ICP备 测试1234567890号-1 </a>
      <img src="http://www.beian.gov.cn/img/new/gongan.png" style="display: inline-block;"><a href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=%E6%B5%8B%E8%AF%951234567890" rel="noopener" target="_blank">京公网安备 测试1234567890号 </a>
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">刀客林</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">41k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">37 分钟</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.0/lib/anime.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>


  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>











<script>
document.querySelectorAll('.pdfobject-container').forEach(element => {
  let url = element.dataset.target;
  let pdfOpenParams = {
    navpanes : 0,
    toolbar  : 0,
    statusbar: 0,
    pagemode : 'thumbs',
    view     : 'FitH'
  };
  let pdfOpenFragment = '#' + Object.entries(pdfOpenParams).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join('&');
  let fullURL = `/lib/pdf/web/viewer.html?file=${encodeURIComponent(url)}${pdfOpenFragment}`;

  if (NexT.utils.supportsPDFs()) {
    element.innerHTML = `<embed class="pdfobject" src="${url + pdfOpenFragment}" type="application/pdf" style="height: ${element.dataset.height};">`;
  } else {
    element.innerHTML = `<iframe src="${fullURL}" style="height: ${element.dataset.height};" frameborder="0"></iframe>`;
  }
});
</script>


<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8.6.4/dist/mermaid.min.js', () => {
    mermaid.init({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    }, '.mermaid');
  }, window.mermaid);
}
</script>


  

  

  <script>
  NexT.utils.loadComments('#SOHUCS', () => {
    NexT.utils.getScript('https://changyan.sohu.com/upload/changyan.js', () => {
      window.changyan.api.config({
        appid: 'cyuYT0YXi',
        conf : '84caa3b92fd5f3996f4e117aad400242'
      });
    });
  });
  </script>
  <script src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>

</body>
</html>
